<?php

namespace app\logic\model\user;

use app\logic\model\LogicModel;
use app\logic\single\GoodsSingle;

class UserGoodsLogicModel extends LogicModel
{
    protected $name='user_goods';
    protected $updateTime=false;

    function getList($user_id =0,$page=1,$limit=15,bool $writer_rate=false,bool $pan = false){
        $query = $this->where('user_id',$user_id);
        $list = $query
            ->page($page,$limit)
            ->order('id desc')
            ->select()->toArray();
        $count = $query->count();
        if(!empty($list)){
            $list = GoodsSingle::getInstance()->getGoodsList($list,$writer_rate,$pan);
        }
        return [$list,$count];
    }
    static function isHaves($arr,$user_id){
        foreach ($arr as &$val){
            $val['flag'] = self::isHave(
                $arr['goods_id']??0,
                $arr['type']??0,
                $user_id??0
            );
        }
        unset($val);
        return $arr;
    }
    static function isHave($goods_id,$type,$user_id): bool
    {
        if(empty($goods_id) || empty($type) || empty($user_id)){
            return false;
        }
        return (bool)self::where('goods_id', $goods_id)
            ->where('type', $type)
            ->where('user_id', $user_id)
            ->find();
    }
}